package com.cl.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.cl.service.impl.QuitServiceImpl;
import com.cl.utils.MegUtil;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class QuitController {
    @Autowired
    private QuitServiceImpl quitService;

    @RequestMapping("/queryallquit")
    public String queryAllQuit(@RequestParam("page") int page, @RequestParam("limit") int limit){
        List<Map<String, Object>> maps = quitService.queryByUquit(1);
        List<Map<String, Object>> maps1 = new ArrayList<>();
        int size = maps.size();
        for (int i = ((page-1)*limit); i < limit*page; i++) {
            maps1.add(maps.get(i));
            if (i==size-1){
                break;
            }
        }
        HashMap<String, Object> map = new HashMap<>();
        map.put("count",size);
        map.put("code",0);
        map.put("msg","");
        map.put("data",maps1);
        return JSON.toJSONStringWithDateFormat(map,"yyyy-MM-hh", SerializerFeature.WriteDateUseDateFormat);
    }

    @RequestMapping("/updatequit")
    public String updateQuit(String field, @Param("uid")int uid){
        int quit = 0;
        int i = 0;
        if (field.equals("离职")){
             quit = 1;
           i = quitService.updateQuit(quit, uid);
            if (i==1){
                return JSON.toJSONString(MegUtil.success("修改成功"));
            }else return JSON.toJSONString(MegUtil.fail("修改失败"));
        }else {
            i = quitService.updateQuit(quit,uid);
            if (i==1){
                return JSON.toJSONString(MegUtil.success("修改成功"));
            }else return JSON.toJSONString(MegUtil.fail("修改失败"));
        }
    }


    @RequestMapping("/deletequit")
    public String deleteQuit(@RequestParam("uid")int uid){
        int i = quitService.deleteQuit(uid);
        if (i==1){
            return JSON.toJSONString(MegUtil.success("删除成功"));
        }else return JSON.toJSONString(MegUtil.fail("删除失败"));
    }
}
